<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;

class Home extends Controller
{
    protected $middleware = ['Auth'];

    //首页视图控制器
    public function index(Request $request)
    {
        return $this->fetch();
    }

    //产品列表视图控制器
    public function fileList(Request $request){
        $where = [];
        $post = input('param.');
        if(isset($post['queryField']) && input('param.queryField') != ''){
            $where['product_no|model'] = input('param.queryField');
            $this->assign('queryFieldWhere', input('param.queryField'));
        }

        if(isset($post['status']) && input('param.status') != ''){
            $where['status'] = input('param.status');
            $this->assign('statusWhere', input('param.status'));
        }

        if($request->userInfo['role'] == 3)
            $where['status'] = 1;
        $productList = Db::name('product')->where($where)->order('id','desc')->select();
        $this->assign('productList',$productList);
        return $this->fetch();
    }

    //下发记录视图控制器
    public function issueList(Request $request){
        $where = [];
        if($request->userInfo['role'] == 3)
            $where['user_id'] = $request->userInfo['id'];

        $issueList = Db::name('issue')
        ->alias('i')
        ->join('user u','i.user_id = u.id')
        ->field('i.*,u.nickname')
        ->where($where)
        ->order('i.id','desc')->select();
        $this->assign('issueList',$issueList);
        return $this->fetch();
    }

    public function userList(Request $request){
        if($request->userInfo['role'] != 1){
            return $this->error('权限不足','/index/index/login');
        }
        $userList = Db::name('user')->order('id','desc')->select();
        $this->assign('userList',$userList);
        return $this->fetch();
    }

    public function getFile(){
        $param = input('param.');
        return download('../uploads/'.$param['url'], $param['name']);
    }
}
